<?php


namespace app\api\controller\order;


use app\api\controller\Auth;
use app\common\business\Order;
use app\common\lib\Show;
use think\Exception;

class index extends Auth
{
    public function save()
    {
        $address_id = input('address_id', 0, 'intval');
        $sku_ids = input('ids', '', 'trim');

        $data = [
            'address_id' => $address_id,
            'sku_ids' => $sku_ids,
            'user_id' => $this->user_id,
        ];

        try {
            $res = (new Order())->save($data);
        } catch (Exception $e) {
            return Show::error([], $e->getMessage());
        }

        if (!$res) {
            return Show::error([], '订单提交失败');
        }
        return Show::success($res);
    }

    public function read()
    {
        $id = input('id', 0, 'intval');
        if (empty($id)) {
            Show::error();
        }

        $data = [
            'order_id' => $id,
            'user_id' => $this->user_id,
        ];
        $res = (new Order())->detail($data);
        if (!$res) {
            return Show::error();
        }
        return Show::success($res);
    }
}